<?php
abstract class Query{
    abstract protected function record(array $data);
    public function select(){
        return $this->record(['name'=>'后盾人','age'=>33,'mobile'=>17691107518]);
    }
}

class Model extends Query{
    #设置字段
    protected $field = [];

    #对外的接口
    public function all(){
        $this->select();
        return $this->field;
    }

    protected function record(array $data)
    {
        $this->field = $data;
        // TODO: Implement record() method.
    }

    public function __get($name)
    {
        if(isset($this->field[$name])){
            return $this->field[$name];
        }
        throw new Exception('暂无数据');
        // TODO: Implement __get() method.
    }

    public function __set($name, $value)
    {
        if(isset($this->field[$name])){
            $this->field[$name] = $value;
        }else{
            throw new Exception('暂无参数');
        }
        // TODO: Implement __set() method.
    }

    public function __unset($name)
    {
        if(!isset($this->field[$name])){
            throw new Exception("禁止操作");
        }
        // TODO: Implement __isset() method.
    }

    public function __isset($name)
    {
        return isset($this->field[$name]);
        // TODO: Implement __isset() method.
    }


    //当调用对象里面的不存在的属性的时候会自动进行调用
    public function __call($name, $arguments)
    {
        $action = 'getAttribute'.ucfirst($name);
        if(method_exists($this,$action)){
                return call_user_func_array([$this,$action],$arguments);
        }
        // TODO: Implement __call() method.
    }



    //当调用不存在的静态方法的时候会自动调用
    public static function __callStatic($name,$arguments){
        switch ($name){
            case 'getAll':
                return call_user_func_array([new self(),'all'],$arguments);
                break;
            default:
                $obj = new static();
                $obj->all();
                return call_user_func([$obj,'__call'],$name,$arguments);
        }
    }

}


class User extends Model{
    //对数据库中的数据进行预处理
    public function getAttributeMobile($len=9){
        return substr($this->field['mobile'],0,$len).'****';
    }
}



try {
//    $model = new Model();
//    $model->all();
//    var_dump($model->name);
//    $model->name = 'daonalishuonalihua';
//    var_dump($model->names);
//    unset($model->names);

//    $user = new User();
//    $user->all();
//    echo $user->mobile();
//    $model = User::getAll();
    $model = User::mobile();
    var_dump($model);
}catch (Exception $e){
    echo $e->getMessage();
}
